var utils = require("../utils/utils");
var ocr_utils = require("../utils/ocr_utils");
var ad = require("./ad");
var in_task = require("./inTask");
var point_map = require("./pointMap");
var FANKA_CONST_AD_TIME = 40;
var fanka = {
  is_over_today: false,
  go_page_from_home() {
    var logger = utils.create_logger("fanka go_page_from_home: ");
    if (ocr_utils.screen_exists_text_all(["视频", "小说"], true)) {
      logger("");
      utils.click_point.apply(
        utils,
        utils.get_size_point(point_map["首页任务"])
      );
      if (this.is_over_today) {
        logger("今天的-翻卡任务-已完成");
        this.next();
        return;
      }
      sleep(utils.get_random_interval(1 * 1000, 0, 1000, 1));
      this.init();
    } else {
      logger("NOT FOUND");
    }
  },
  init() {
    var logger = utils.create_logger("fanka init: ");
    in_task.init(null, null, null, null, null, null, 3);
    var ocr_text = ocr_utils.find_text(
      "翻卡赢",
      false,
      null,
      10,
      (device.height / 10) * 4
    );
    if (ocr_text) {
      var centerX = ocr_text.bounds.centerX();
      var centerY = ocr_text.bounds.centerY();
      var result = this.click_times(ocr_text, centerX, centerY);
      if (!result) {
        logger("没找到-|再|翻|倍|-，跳过翻卡");
        return;
      }
      logger("找到-|再|翻|倍|-");
      utils.click_ocr_text(result);
      var ad_interval = utils.get_random_interval(FANKA_CONST_AD_TIME, 0, 5, 1);
      this.watch_ad(ad_interval);
      back();
      sleep(utils.get_random_interval(5, 0, 2, 1000));
      var ocr_text = ocr_utils.screen_exists_text_one("直接领取", true);
      if (ocr_text) {
        logger("找到-|直|接|领|取|-");
        utils.click_ocr_text(ocr_text);
        sleep(utils.get_random_interval(1, 0, 1, 1000));
      }
      if (utils.click_text_starts_with("个位卡")) {
        sleep(utils.get_random_interval(2, 0, 2, 1000));
        this.run();
        return;
      }
    } else {
      logger("没找到-|翻|卡|-，今天的-|翻|卡|-已完成");
      this.is_over_today = true;
      this.after();
    }
  },
  click_times(ocr_text, centerX, centerY) {
    var logger = utils.create_logger("fanka click_times: ");
    if (!(centerY < parseInt((device.height / 2400) * 1846))) {
      logger(
        "找到--" + ocr_text.text + "-",
        "防止和开宝箱冲突，将向上再滑动-" + (device.height / 10) * 3 + "-"
      );
      utils.swipe(1 / 2, (1 / 10) * 5, 1 / 2, (1 / 10) * 2);
      sleep(utils.get_random_interval(1000, 0, 1000, 1));
      centerY = centerY - (device.height / 10) * 3;
    }

    var DELTA_X = 0;
    var DELTA_Y = 207;
    //这个delta是在1080*2400设备上取得
    var rate_x = device.width / 1080;
    var rate_y = device.height / 2400;
    var x = centerX + DELTA_X * rate_x;
    var y = centerY + DELTA_Y * rate_y;
    utils.click_x_y(x, y);
    sleep(utils.get_random_interval(2, 0, 2, 1000));
    return ocr_utils.screen_exists_text_one(["再翻倍"], true);
  },
  watch_ad(ad_interval) {
    console.show();
    var logger = utils.create_logger("fanka watch_ad");
    logger(ad_interval + "秒");
    var if_current_ad_is_over_interval = 2; //每隔2s检查当前广告是否已经结束
    var is_over_early = false;
    while (ad_interval > if_current_ad_is_over_interval) {
      if (utils.exists_text_starts_with("已获得")) {
        var before_over_interval = random(2, 5);
        logger(
          "找到-已获得-，广告已结束,再过" +
            before_over_interval +
            "s后停止当前广告"
        );
        sleep(before_over_interval * 1000);
        is_over_early = true; //广告实际比预计的时间提前结束
        break;
      }
      sleep(if_current_ad_is_over_interval * 1000);
      ad_interval = ad_interval - if_current_ad_is_over_interval;
      logger("广告时间预计还剩" + ad_interval + "秒");
    }
    if (is_over_early) {
      console.hide();
      sleep(1 * 1000);
      return is_over_early;
    }
    if (ad_interval > 0) {
      logger("广告时间还剩最后的" + ad_interval + "秒");
      sleep(ad_interval * 1000);
      console.hide();
      sleep(1 * 1000);
      return is_over_early;
    }
  },
  go_back() {
    var logger = utils.create_logger("fanka go_back: ");
    back();
    sleep(utils.get_random_interval(5, 0, 2, 1000));
    var ocr_text = ocr_utils.screen_exists_text_one("直接领取", true);
    if (ocr_text) {
      logger("找到-|直|接|领|取|-");
      utils.click_ocr_text(ocr_text);
      sleep(utils.get_random_interval(1, 0, 1, 1000));
      return;
    }
    var ocr_text = ocr_utils.screen_exists_text_one("看视频可再", true);
    if (ocr_text) {
      logger("找到-|看|视|频|可|再|翻|一|次|-");
      utils.click_ocr_text(ocr_text);
      sleep(utils.get_random_interval(2, 0, 2, 1000));
      var ad_interval = utils.get_random_interval(FANKA_CONST_AD_TIME, 0, 5, 1);
      this.watch_ad(ad_interval);
      this.go_back();
      return;
    }
    logger("没找到-|看|视|频|可|再|翻|一|次|-");
    this.run();
    return;
  },
  run() {
    var ocr_text = ocr_utils.screen_exists_text_one("看视频再", true);
    if (ocr_text) {
      console.log("fanka run: 找到-|看|视|频|再|-");
      utils.click_ocr_text(ocr_text);
      sleep(utils.get_random_interval(2, 0, 2, 1000));
      var ad_interval = utils.get_random_interval(FANKA_CONST_AD_TIME, 0, 5, 1);
      this.watch_ad(ad_interval);
      this.go_back();
      return;
    } else {
      console.log("fanka run: 没找到-|看|视|频|再|-");
      this.after();
    }
  },
  back_top(times) {
    var logger = utils.create_logger("fanka back_top: ");
    logger("下面开始回到顶部，将向上滑动--" + times + "--次");
    while (times > 0) {
      swipe(
        (device.width / 2) * utils.get_swipe_times(95, 5),
        (device.height / 3) * 1 * utils.get_swipe_times(95, 5),
        (device.width / 2) * utils.get_swipe_times(95, 5),
        (device.height / 3) * 2 * utils.get_swipe_times(95, 5),
        utils.get_random_interval(200, 0, 500, 1)
      );
      times = times - 1;
      console.log("还需要向上滑动--" + times + "--次");
      sleep(utils.get_random_interval(1, 0, 1, 1000));
    }
  },
  after() {
    console.log("fanka after:");
    var ocr_text = ocr_utils.screen_exists_text_one("马上收下", true);
    if (ocr_text) {
      console.log("fanka after: 找到-|马|上|收|下|-");
      utils.click_ocr_text(ocr_text);
      sleep(utils.get_random_interval(2, 0, 2, 1000));
    }
    this.next();
  },
  next() {
    this.back_top(6);
    console.log("fanka next");
  },
};
module.exports = fanka;
